from manim import *

class CustomCurve10(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-20, -5, 0],
            [-16,-1,0],
            
        ]
        self.set_points_as_corners(points)#直线

class CustomCurve11(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-16,-1,0],
            
            [-20,5,0]
        ]
        self.set_points_smoothly(points)
class CustomCurve12(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-20,5,0],
            
            [-24,-1,0]
        ]
        self.set_points_smoothly(points)
class CustomCurve13(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
           [-24,-1,0],
            
            [-20,-5,0]
        ]
        self.set_points_as_corners(points)


class CustomCurve8(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-20, -5, 0],
            [-16,-2.5,0],
            [-20,5,0]
        ]
        self.set_points_smoothly(points)

class CustomCurve9(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-20, 5, 0],
            [-24,-2.5,0],
            [-20,-5,0]
        ]
        self.set_points_smoothly(points)




class CustomCurve(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-40, 0, 0],
            [-35,0,0],
            [-35,-5,0]
        ]
        self.set_points_smoothly(points)
class CustomCurve7(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-40, -5, 0],
            [-35, -5, 0],
            [-30,0,0],
            [-35, 0, 0],
            [-40,-5,0]
        ]
        self.set_points_smoothly(points)
class CustomCurve1(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-40, 3, 0],
            [-32,3,0],
            [-32,-5,0]
        ]
        self.set_points_smoothly(points)
class CustomCurve5(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-37, 3.5, 0],
            [-32,3,0],
            [-31.5,-2,0]
        ]
        point1s = [
            [-31.5, -2, 0],
            [-34.5,-3,0],
            
        ]
        point2s = [
            [-34.5,-3,0],
            [-35,0,0],
            [-38,0.5,0]
        ]
        point3s = [
            [-38,0.5,0],
            [-37, 3.5, 0],
            
        ]
        self.set_points_smoothly(points)
        
class CustomCurve6(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-37, 3.5, 0],
            [-32,3,0],
            [-31.5,-2,0]
        ]
        point1s = [
            [-31.5, -2, 0],
            [-34.5,-3,0],
            
        ]
        point2s = [
            [-34.5,-3,0],
            [-35,0,0],
            [-38,0.5,0]
        ]
        point3s = [
            [-38,0.5,0],
            [-37, 3.5, 0],
            
        ]
       
        self.set_points_as_corners(point1s)
        
class CustomCurve2(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-37, 3.5, 0],
            [-32,3,0],
            [-31.5,-2,0]
        ]
        point1s = [
            [-31.5, -2, 0],
            [-34.5,-3,0],
            
        ]
        point2s = [
            [-34.5,-3,0],
            [-35,0,0],
            [-38,0.5,0]
        ]
        point3s = [
            [-38,0.5,0],
            [-37, 3.5, 0],
            
        ]
        
        self.set_points_smoothly(point2s)
       
class CustomCurve3(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-37, 3.5, 0],
            [-32,3,0],
            [-31.5,-2,0]
        ]
        point1s = [
            [-31.5, -2, 0],
            [-34.5,-3,0],
            
        ]
        point2s = [
            [-34.5,-3,0],
            [-35,0,0],
            [-38,0.5,0]
        ]
        point3s = [
            [-38,0.5,0],
            [-37, 3.5, 0],
            
        ]
        
        self.set_points_as_corners(point3s)
class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text0 = Tex(r"\text{(张1)设}$x\geq 0,y\geq 0$\text{,曲线}$L_{1}:x^{2}+y^{2}-xy=1,L_{2}:x^{2}+y^{2}-xy=2$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,20,0])
        text00= Tex(r"\text{,直线}$L_{3}:y=\dfrac{\sqrt{3}}{3}x,L_{4}:y=\sqrt{3}x$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text0,DOWN,buff=1)
        text1 = Tex(
                   r"\text{区域}$D$\text{由}$L_{1},L_{2},L_{3},L_{4}$\text{围成}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text00,DOWN,buff=1)
        text2 = Tex(
                    r"\text{(张2)已知曲线}$L$\text{的极坐标方程为}$r=\sin 3\theta \left( 0\leq \theta \leq \dfrac{\pi }{3}\right) $\text{,}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        text20 = Tex(
                    r"$D$\text{为曲线}$L$\text{围成的区域}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text2,DOWN,buff=1)
        text3 = Tex(
                   r"\text{(张3)已知平面区域}$D=\left\{  \left( x,y\right) \left| \right| x| \leq y,\left( x^{2}+y^{2}\right) ^{3}\leq y^{4}\right\} $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-15,0])
        

        self.play(Write(text0),Write(text00),Write(text1),Write(text2),Write(text20),Write(text3))

        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        

        self.play(text2.animate().scale(1).shift(DOWN*40),text20.animate().scale(1).shift(DOWN*40),text3.animate().scale(0.8).shift(DOWN*40)) 
        
        text4 = Tex(
                   r"\text{对称性+取特殊点}",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([0,0,0])
        self.play(Write(text4))
        text5 = Tex(
                   r"\text{对称性+取特殊点}",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,25,0])
        self.play(
            ReplacementTransform(text4,text5)
        )
        self.wait(0.2)

        # 创建坐标系
        axes = Axes(
            x_range=[-3, 10, 3],  # 设置 x 轴范围，从 -π 到 π，步长为 π
            y_range=[-3, 10, 3],  # 设置 y 轴范围，从 -1.5 到 1.5，步长为 0.5
            axis_config={"color": WHITE, "stroke_width": 30},  # 设置坐标轴的颜色和线宽
            x_length=20,  # 控制 x 轴长度
            y_length=20,  # 控制 y 轴长度
        )
        axes.move_to([-35, 0, 0])  # 移动坐标系的位置

        # 绘制 1/x 函数图像
        # 分为两部分以避开 x=0
        #graph_left = axes.plot(lambda x: 5/x, color=BLUE, stroke_width=30, x_range=[-5, -0.1])
        #graph_right = axes.plot(lambda x: 5/x, color=PURE_RED, stroke_width=30, x_range=[0.1, 10])
        
       

        title = Tex(
                    r"$f\left( x\right) $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,14,0])

       


        #self.play(Create(arrow10),Create(text15))
        # 将所有元素添加到场景中
        #self.play(Create(axes), Create(labels), Create(graph2))
         # 显示图像
        curve = CustomCurve(color=WHITE, stroke_width=20)
        
        self.play(Create(axes))


        text6 = Tex(
                   r"\text{轮换性}$\Rightarrow $\text{曲线关于}$y=x$\text{对称}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(Write(text6))


        #下划线
        #下划线
        dot1=Dot([-45,-10,0])
        dot2=Dot([-25,10,0])
        line=Line(dot1,dot2).set_color(WHITE)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))
        text7 = Tex(
                   r"$x=0\Rightarrow y=1$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-5,0])
        self.play(Write(text7))
        dot3 = Dot([-40, 0, 0], color=RED, radius=0.5)
        self.play(Create(dot3))
        text8 = Tex(
                   r"$x=1\Rightarrow y=1$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        self.play(Write(text8))
        dot4 = Dot([-35, 0, 0], color=RED, radius=0.5)
        self.play(Create(dot4))
        text9 = Tex(
                   r"$x=1\Rightarrow y=0$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-15,0])
        self.play(Write(text9))
        dot5 = Dot([-35, -5, 0], color=RED, radius=0.5)
        self.play(Create(dot5))
        self.play( Create(curve))


        text10 = Tex(
                   r"$x=0\Rightarrow y=\sqrt{2}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-5,0])
        self.play(Write(text10))
        dot6 = Dot([-40, 3, 0], color=GREEN, radius=0.5)
        self.play(Create(dot6))
        text11 = Tex(
                   r"$x=\sqrt{2}\Rightarrow y=\sqrt{2}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-10,0])
        self.play(Write(text11))
        dot7 = Dot([-32, 3, 0], color=GREEN, radius=0.5)
        self.play(Create(dot7))
        text12 = Tex(
                   r"$x=\sqrt{2}\Rightarrow y=0$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-15,0])
        self.play(Write(text12))
        dot8 = Dot([-32, -5, 0], color=GREEN, radius=0.5)
        self.play(Create(dot8))
        curve1 = CustomCurve1(color=WHITE, stroke_width=20)
        
        self.play(  Create(curve1))

       

        


        dot1=Dot([-40,-5,0])
        dot2=Dot([-25,0,0])
        line1=Line(dot1,dot2).set_color(WHITE)
        line1.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line1))

        dot1=Dot([-40,-5,0])
        dot2=Dot([-35,10,0])
        line2=Line(dot1,dot2).set_color(WHITE)
        line2.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line2))


        curve2 = CustomCurve2(color=PURE_RED, stroke_width=60)
        curve3 = CustomCurve3(color=PURE_RED, stroke_width=60)
        
        curve5 = CustomCurve5(color=PURE_RED, stroke_width=60)
        curve6 = CustomCurve6(color=PURE_RED, stroke_width=60)
        self.play(Create(curve5),Create(curve6),Create(curve3),Create(curve2))
        self.wait(0.2)
        self.play(Uncreate(text0),Uncreate(text00),Uncreate(text1),Uncreate(text6)
             ,Uncreate(text7),Uncreate(text8),Uncreate(text9),Uncreate(text10)
             ,Uncreate(text11),Uncreate(text12),Uncreate(line1),Uncreate(line)
             ,Uncreate(line2),Uncreate(dot3),Uncreate(dot4),Uncreate(dot5),Uncreate(dot6)
             ,Uncreate(dot7),Uncreate(dot8),Uncreate(curve),Uncreate(curve1),Uncreate(curve2),Uncreate(curve3)
             ,Uncreate(curve5),Uncreate(curve6))
        self.play(text2.animate().scale(1).move_to([0,20,0]),text20.animate().scale(1).move_to([0,15,0])) 

        
        text7 = Tex(
                   r"$\theta =0\Rightarrow r=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,5,0])
        self.play(Write(text7))
        dot3 = Dot([-40, -5, 0], color=RED, radius=0.5)
        self.play(Create(dot3))


        text8 = Tex(
                   r"$\theta =\dfrac{\pi }{6}\Rightarrow r_{\max }=1$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-5,0])
        self.play(Write(text8))
        dot1=Dot([-40,-5,0])
        dot2=Dot([-20,5,0])
        line=Line(dot1,dot2).set_color(WHITE)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))
        dot4 = Dot([-30, 0, 0], color=RED, radius=0.5)
        self.play(Create(dot4))
        text9 = Tex(
                   r"$\theta =\dfrac{\pi }{3}\Rightarrow r=0$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-15,0])
        self.play(Write(text9))
        dot1=Dot([-40,-5,0])
        dot2=Dot([-35,5,0])
        line1=Line(dot1,dot2).set_color(WHITE)
        line1.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line1))
        dot5 = Dot([-40, -5, 0], color=RED, radius=0.5)
        self.play(Create(dot5))

        curve7 = CustomCurve7(color=PURE_RED, stroke_width=60)
        
        
        self.play(  Create(curve7))


        self.play(
             Uncreate(text7),Uncreate(text8),Uncreate(text9)
           ,Uncreate(line1),Uncreate(line)
             ,Uncreate(dot3),Uncreate(dot4),Uncreate(dot5)
             ,Uncreate(text2),Uncreate(text20)
             ,Uncreate(curve7))
        self.play(text3.animate().scale(1).move_to([0,20,0])) 



        # 创建坐标系
        axes1 = Axes(
            x_range=[-10, 10,5 ],  # 设置 x 轴范围，从 -π 到 π，步长为 π
            y_range=[-3, 10, 3],  # 设置 y 轴范围，从 -1.5 到 1.5，步长为 0.5
            axis_config={"color": WHITE, "stroke_width": 30},  # 设置坐标轴的颜色和线宽
            x_length=20,  # 控制 x 轴长度
            y_length=20,  # 控制 y 轴长度
        )
        axes1.move_to([-20, 0, 0])  # 移动坐标系的位置


        self.play(
            ReplacementTransform(axes,axes1)
        )
        self.wait(0.2)


        dot1=Dot([-20,-5,0])
        dot2=Dot([-10,5,0])
        line1=Line(dot1,dot2).set_color(WHITE)
        line1.set_stroke(width=30)  # 设置宽度为 30
        
        dot3=Dot([-30,5,0])
        dot4=Dot([-20,-5,0])
        line2=Line(dot3,dot4).set_color(WHITE)
        line2.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line2))
        self.play(Create(line1))




        text9 = Tex(
                   r"\text{令}$\begin{cases}x=r\cos \theta \\y=r\sin \theta \end{cases}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,10,0])
        self.play(Write(text9))

        text10 = Tex(
                   r"$\Rightarrow r=\sin ^{2}\theta $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,0,0])
        self.play(Write(text10))

        text11 = Tex(
                   r"$\theta =0\Rightarrow r=0$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-10,0])
        self.play(Write(text11))
        dot5 = Dot([-20, -5, 0], color=RED, radius=0.5)
        self.play(Create(dot5))
        text12 = Tex(
                   r"$\theta =\dfrac{\pi }{2}\Rightarrow r_{\max }=1$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-20,0])
        self.play(Write(text12))
        dot5 = Dot([-20, 5, 0], color=RED, radius=0.5)
        self.play(Create(dot5))

        curve8 = CustomCurve8(color=WHITE, stroke_width=20)
        
        
        self.play(  Create(curve8))

        curve9 = CustomCurve9(color=WHITE, stroke_width=20)
        
        
        self.play(  Create(curve9))

        curve10 = CustomCurve10(color=PURE_RED, stroke_width=60)
        curve11 = CustomCurve11(color=PURE_RED, stroke_width=60)
        curve12 = CustomCurve12(color=PURE_RED, stroke_width=60)
        curve13 = CustomCurve13(color=PURE_RED, stroke_width=60)
        
        
        self.play(  Create(curve10),Create(curve11),Create(curve12),Create(curve13))


























        self.wait()

    

      
        
       

        
       


      



        